/**
 * created by 江心才子, 2019/8/5
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.home.timetable

import androidx.databinding.ObservableArrayList
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wyl.base.*
import com.wyl.base.bean.VideoData
import com.wyl.base.init.registrationID
import com.wyl.libbase.base.BaseViewModel

/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-05 13:42
 * 内容描述：
 * 修改说明：
 */
class TimeTableViewModel : BaseViewModel() {
    val week = ObservableArrayList<VideoData>()
    val nextWeek = ObservableArrayList<VideoData>()

    init {
        getData()
    }

    private fun getData() {
        OkGo.post<BaseResponse<List<VideoData>>>(HOME_TIMETABLE)
            .params("appUserId", ACacheHelper.id)
            .converter(object : JsonConvert<BaseResponse<List<VideoData>>>() {})
            .adapt(ObservableBody<BaseResponse<List<VideoData>>>())
            .doOnSubscribe { loading.value = true }
            .doFinally { loading.value = false }
            .map { it.data }
            .subscribe(
                { data ->
                    data.forEach {
                        if (it.type == "1") {
                            week.add(it)
                        } else if (it.type == "2") {
                            nextWeek.add(it)
                        }
                    }
                }, {
                    onError(it)
                }
            ).toDisposables(disposables)
    }

    fun collect(videoData: VideoData) {
        if (videoData.status == 1) {
            // 收藏
            OkGo.post<BaseResponse<Any>>(CommonCollect)
                .params("appUserId", ACacheHelper.id)
                .params("appFolkOperaId", videoData.appFolkOperaId)
                .converter(object : JsonConvert<BaseResponse<Any>>() {})
                .adapt(ObservableBody<BaseResponse<Any>>())
                .doOnSubscribe { loading.value = true }
                .doFinally { loading.value = false }
                .subscribe(
                    { data ->
                        toast.value = data.msg
                        videoData.status = 2
                    }, {
                        onError(it)
                        it.printStackTrace()
                    }
                ).toDisposables(disposables)
        } else {
            // 取消收藏
            OkGo.post<BaseResponse<Any>>(CommonCancelCollect)
                .params("appUserId", ACacheHelper.id)
                .params("appFolkOperaId", videoData.appFolkOperaId)
                .converter(object : JsonConvert<BaseResponse<Any>>() {})
                .adapt(ObservableBody<BaseResponse<Any>>())
                .doOnSubscribe { loading.value = true }
                .doFinally { loading.value = false }
                .subscribe(
                    { data ->
                        toast.value = data.msg
                        videoData.status = 1
                    }, {
                        onError(it)
                        it.printStackTrace()
                    }
                ).toDisposables(disposables)
        }

    }

    fun push() {
        if (registrationID.isEmpty()) {
            error.value = "设置失败"
            return
        }
        OkGo.post<BaseResponse<Any>>(HOME_PUSH)
            .params("appUserId", ACacheHelper.id)
            .params("type", "2")
            .params("token", registrationID)
            .converter(object : JsonConvert<BaseResponse<Any>>() {})
            .adapt(ObservableBody<BaseResponse<Any>>())
            .subscribe {
                ACacheHelper.cache.put(PUSH, true)
            }.toDisposables(disposables)
    }
}